// pages/detail/detail.js
const util = require("../../../utils/util.js")
Page({  
  /**
   * 页面的初始数据
   */
  data: {
    headImg: [ 
      "/image/index_08.png",
      "/image/index_08.png",
      "/image/index_08.png"
    ],
    ImgUrl: 'https://hsh.bakebao.com',
    current:0,
    photo:false,
    hide:false, 
    lock: false,
    boolean: true,
    hint: true,  
    mask:0,
    sortId:0,
    indexSize: 0,
    background:"#f6f6f6",
    height:"100%",
    indicatorDots: false,
    autoplay: false,
    duration: 0, //可以控制动画
    list: '', 
  },
  previmage: function (e) {
    var that = this;  
    that.GetSupplierByID(that.data.SID, function (data) {
    wx.previewImage({
        current: data.imgs[0], // 当前显示图片的http链接
        urls: data.imgs // 需要预览的图片http链接列表
      })
    })
  },
  swiperChange:function(e){
    this.setData({
      current: e.detail.current
    })
  },  
  touchend: function () {
    if (this.data.lock) {
      //开锁
      setTimeout(() => {
        this.setData({ lock: false });
      }, 100);
    }
  },
  //收藏
  storeCollect:function(e){
    var that = this;
    var SID = e.currentTarget.dataset.sid;  //获取供应商ID
    var UID = wx.getStorageSync('UserID') || []//获取本地储存的 用户信息    
    if (UID!=""){      
      that.UpdateStoreBySIDAndUID(SID, UID);
    } else {
      util.GetUserInfo("您点击了拒绝授权,将无法收藏店铺信息,点击确定重新获取授权。"); //获取用户ID
      UID = wx.getStorageSync('UserID') || []
      console.log(UID);
      if (UID != "") {
        that.UpdateStoreBySIDAndUID(SID, UID);
      }
    }    
  },
  intMask:function(){
    this.setData({mask:1})
  },
  goMap:function(){
    var that = this;
    wx.openLocation({
      latitude: that.data.SupplierInfo.Lat,
      longitude: that.data.SupplierInfo.Lng,
      name: that.data.SupplierInfo.ShopName,
      address: that.data.SupplierInfo.Address
    })
    // return;
    // wx.navigateTo({
    //   url: '../map/map',
    // })
  },
  closeMask:function(){
    this.setData({ mask: 0 })
  },
  calling: function () {
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.SupplierInfo.Mobile, 
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },
  sortClick:function(e){
    this.setData({
      sortId: e.currentTarget.dataset.index
    });
  },

  change(e) {
    console.log(e);
    this.setData({
      indexSize: e.detail.current
    })
  },

  scrollTo(e) {   
    var that = this;
    var categoryid = e.target.dataset.categoryid;
    var categorytitle = e.target.dataset.categorytitle;
    this.setData({
      indexSize: e.target.dataset.index
    })
    that.GetProductListByCategoryID(categoryid, categorytitle);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.ShowLoading();//显示加载Loading
    var sid = options.sid;
    that.setData({SID:sid});
    var userid = wx.getStorageSync('UserID') || []
    if(sid!=0){
      that.GetSupplierByID(sid,function(){}); //获取商户信息
      that.GetProductBySID(sid,function(){}); //根据供应商ID获取商品信息
      that.GetCategoryBySID(sid); //根据ID获取分类信息
      that.GetStoreBySIDAndUID(sid, userid);
      that.setData({ sid: sid });
    }else{
      wx.navigateBack({
        delta: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  ShowLoading: function () {
    this.setData({
      hiddenLoading: false
    });
    var that = this;
    setTimeout(function () {
      wx.stopPullDownRefresh();
      that.setData({
        hiddenLoading: true
      });
    }, 1500);
  },
  GetSupplierByID: function (sid, callback){  //根据ID获取商户信息    
    var that = this;
    var data = {
      id:sid
    };
    data.act = "Find_SupplierByID";
    util.Ajax(data, function (json) {
      if (json.data.ID > 0) {
        that.setData({ SupplierInfo: json.data });
        callback(json.data);
      }
    });
  },
  GetProductBySID: function (sid,callback) { //根据商户ID获取商品信息
    var that = this;
    var data = {
      sid: sid
    };
    data.act = "Find_ProductBySID";
    util.Ajax(data, function (json) {
      if (json.count > 0) {        
        that.setData({ ProductList: json.data });
        callback(json.data);
      }
    });
  },
  GetCategoryBySID:function(sid){ //根据ID获取分类信息
    var that = this;
    var data = {
      sid: sid
    };
    data.act = "Find_CategoryBySID";
    util.Ajax(data, function (json) {      
      if (json.count > 0) {
        that.setData({ CategoryList: json.data });

        var cid = that.data.CategoryList[0].ID; //获取到分类信息，取出第一个分类 显示页面
        var title = that.data.CategoryList[0].Name;
        that.GetProductListByCategoryID(cid,title);
      }else{
        that.setData({ background: "#fff", height: 0, hint: false})
      }
    });
  },
  GetProductListByCategoryID: function (cid, categorytitle){ //根据分类ID获取商品
    var that = this;
    var ProductsList = new Array();
    that.GetProductBySID(that.data.sid, function (ProductList) {
      for (var i = 0; i < ProductList.length; i++) {
        if (cid == -1) {  //当分类等于 热销时
          if (ProductList[i].Tags.includes("热销")) {
            ProductsList.push(ProductList[i]);
          }
        } else if (cid == -2) { //当分类等于 折扣时
          if (ProductList[i].Tags.includes("折扣")) {
            ProductsList.push(ProductList[i]);
          }
        } else {
          if (ProductList[i].CategoryID == cid) {
            ProductsList.push(ProductList[i]);
          }
        }
      }
      that.setData({ ProductsList: ProductsList, CategoryTitle: categorytitle });
      wx.hideLoading();
    });
  },
  UpdateStoreBySIDAndUID: function (sid, uid) { //用户收藏or取消 商户    
    var that = this;
    var data = {
      supplierid: sid,
      userid: uid
    };
    data.act = "Save_Store";
    util.Ajax(data, function (json) {
      if (json.data > 0) {  //收藏成功   
        util.ShowSuccess("收藏成功");
        that.setData({
          boolean: false
        })
      } else {  //取消收藏
        util.ShowSuccess("取消收藏成功");
        that.setData({
          boolean: true
        })
      }

    });
  },
  GetStoreBySIDAndUID: function (sid, uid) { //根据商户ID和用户ID查询收藏状态    
    var that = this;
    var data = {
      supplierid: sid,
      userid: uid
    };
    data.act = "Find_StoreBySIDAndUID";
    util.Ajax(data, function (json) {
      if (json.data > 0) {  //已收藏     
        that.setData({
          boolean: false
        })
      } else {  //未收藏
        that.setData({
          boolean: true
        })
      }

    });
  }

})